<template>
    <div>

        <div class="title">
            <div class="desc">学术组织任职</div>
            <div>
                <el-button type="primary" @click="handleAdd" :disabled="flag == 3">经历添加</el-button>
            </div>
        </div>

        <el-table :data="membersList" class="table">
            <el-table-column label="开始时间"  width="150" >
                <template #default="scope">
                    <el-date-picker v-if="activeIndex == scope.$index" v-model="scope.row.startTime" type="data"
                                    @change="changeStartTime(scope.row.startTime,scope.row.endTime,scope.$index)"
                        value-format="YYYY-MM-DD" placeholder="开始时间" clearable style="width: 100%" />
                    <span v-else>{{ scope.row.startTime }}</span>
                </template>
            </el-table-column>

            <el-table-column label="结束时间"  width="150" >
                <template #default="scope">
                    <el-date-picker v-if="activeIndex == scope.$index" v-model="scope.row.endTime" type="data"
                                    @change="changeEndTime(scope.row.startTime,scope.row.endTime,scope.$index)"
                        value-format="YYYY-MM-DD" placeholder="结束时间" clearable style="width: 100%" />
                    <span v-else>{{ scope.row.endTime }}</span>
                </template>
            </el-table-column>

            <el-table-column label="组织名称"  width="200" >
                <template #default="scope">
                    <el-input v-if="activeIndex == scope.$index" v-model="scope.row.organizationName"
                        placeholder="请输入组织名称" maxlength="50" show-word-limit ></el-input>
                    <span v-else>{{ scope.row.organizationName }}</span>
                </template>
            </el-table-column>

            <el-table-column label="担任事务"  width="200" >
                <template #default="scope">
                    <el-input v-if="activeIndex == scope.$index" v-model="scope.row.positionTitle"
                        placeholder="请输入担任事务" maxlength="20" show-word-limit ></el-input>
                    <span v-else>{{ scope.row.positionTitle }}</span>
                </template>
            </el-table-column>

<!--
          <el-table-column label="获奖方式"  width="200" >
                <template #default="scope">
                  <el-select v-if="activeIndex == scope.$index" v-model="scope.row.awardMethodId" clearable placeholder="请选择获奖方式"  @change="changeEducate($event, activeIndex)">
                    <el-option :label="item.itemText" :value="item.itemValue" :key="item.itemValue"
                               v-for="item in hjfsList"   />
                  </el-select>
                    <span v-else>{{ scope.row.awardMethod }}</span>
                </template>
            </el-table-column>
-->
            <el-table-column align="right" width="250">
                <template #header>
                    <span>操作</span>
                </template>
                <template #default="scope">
                    <div v-if="activeIndex == scope.$index">

                        <el-button type="success" @click="handleSave(scope.row)">保存</el-button>
                      <el-popconfirm @confirm="handleDelete(scope.$index)" width="220" confirm-button-text="确定"
                                     cancel-button-text="取消" icon-color="#e6a23c" title="你确定要删除当前行?">
                        <template #reference>
                          <el-button type="danger" :disabled="flag == 3">删除</el-button>
                        </template>
                      </el-popconfirm>
                    </div>
                    <div v-else>
                        <el-button type="primary" @click="handleEdit(scope.$index)" :disabled="flag == 3">编辑</el-button>
                        <el-popconfirm @confirm="handleDelete(scope.$index)" width="220" confirm-button-text="确定"
                            cancel-button-text="取消" icon-color="#e6a23c" title="你确定要删除当前行?">
                            <template #reference>
                                <el-button type="danger" :disabled="flag == 3">删除</el-button>
                            </template>
                        </el-popconfirm>
                    </div>
                </template>
            </el-table-column>
        </el-table>
    </div>
</template>



<script  lang="ts">

import { ref, watch } from 'vue';
import { tips } from '@/views/science/components/tips/tips'

export default {


    components: {

    },
    emits: {
        dataChange: null,
        paramsChange: null,
    },
    props: {
        members: {
            type: Array,
            required: true,
        },
      hjfsList: {
        type: Array,
        required: true,
      },
      // falg  1:新增  2:编辑  3:详情
      flag: {
        type: String,
        default: 1,
      },
    },
    setup(props, { emit }) {

        let activeIndex = ref(-1);


        const membersList = ref(props.members)

        watch(props, (newValue, oldValue) => {
            membersList.value = newValue.members
        });

      let  changeEducate=(id:any,currentRow:any)=>{

        let itemText = props.hjfsList.find((item:any) => item.itemValue == id).itemText;

        membersList.value[currentRow].awardMethodId=id

        membersList.value[currentRow].awardMethod=itemText

      }


        const handleAdd = () => {

            let item = {
                startTime: '',
                endTime: '',
                organizationName: '',
                positionTitle: '',
                // awardMethodId: '',
                // awardMethod: '',
            };

            let flag = true
            membersList.value.every(item=>{
              console.log(item,'item');
              for(let key in item){
                if(item[key]===''){
                  flag =  false
                  return false
                }
              }
              return true
            })
            if(!flag){
              tips("请填写完整数据", "warning");

              return
            }

            membersList.value.push(item);

            activeIndex.value = membersList.value.length - 1;
        }

        const handleEdit = (index: number) => {
            activeIndex.value = index;
        };

        const handleSave = (row) => {
            console.log(row,'asdasdas');
            for(let key in row){
                if(row[key]===''){
                console.log(row[key],key,'asdasd');
                
                tips("请填写完整数据", "warning");
                return
                }
            }
            activeIndex.value = -1;

            emit('paramsChange', membersList.value)

        };

        const handleDelete = (index: number) => {
            membersList.value.splice(index, 1);

            emit('paramsChange', membersList.value)
        };
      const changeStartTime = (startTime,endTime,index) =>{
        let start = new Date(startTime).getTime()
        let end = new Date(endTime).getTime()
        if(start > end){
          tips("开始时间需要小于结束时间", "warning");
          membersList.value[index].startTime = ''
        }
        let duration = Math.trunc((end - start) / 2592000000)
        membersList.value[index].duration = duration > 0 ? duration : 0
        // console.log('changeStartTime',duration,membersList.value,index,);
      }
      const changeEndTime = (startTime,endTime,index) =>{
        let start = new Date(startTime).getTime()
        let end = new Date(endTime).getTime()
        if(start > end){
          tips("开始时间需要小于结束时间", "warning");
          membersList.value[index].endTime = ''
        }
        let duration = Math.trunc((end - start) / 2592000000)
        membersList.value[index].duration = duration > 0 ? duration : 0
      }
        return {

            membersList,
            activeIndex,
            handleAdd,
            handleEdit,
            handleSave,
            handleDelete,
          changeStartTime,
          changeEndTime,
          changeEducate,
        }


    },
}
</script>












<style scoped lang="scss">
.title {
    display: flex;
    justify-content: space-between;

    .desc {
        font-weight: bolder;
    }
}

.table {
    margin: 15px 0;
}</style>
